/* eslint-disable no-param-reassign */
import i18n from '@/lang'
import _ from 'lodash'
import serviceConfig from '@/api/modules/software/serviceConfig'
const initData = {
  serviceCode: undefined,
  serviceName: undefined,
  serviceBelong: undefined,
  serviceCategory: undefined,
  serviceExplain: '',
  serviceDesc: '',
  needStudy: 1,
  examUrl: '',
  studyUrls: [''],
  modelDtoList: [],
}
const state = {
  newServiceData: _.cloneDeep(initData),
  rightDrawerEvent: '',
  // 服务下拉框
  serviceTem: [],
  // 是否类型
  chooseYesNoType: [{
    id: 1,
    name: 'yes',
    value: 1
  }, {
    id: 0,
    name: 'no',
    value: 0
  }],
  // 销售方式
  saleStateType: [{
    id: 0,
    name: '标配',
    value: 0
  },/*{
    id: 1,
    name: '售卖',
    value: 1
  },*/
  {
    id: 2,
    name: '选装',
    value: 2
  }
  ],
  activeStateType: [
    {
      id: 0,
      name: '激活中',
      value: 0
    }, {
      id: 1,
      name: '激活成功',
      value: 1
    },{
      id: 2,
      name: '激活失败',
      value: 2
    },/*{
          id: 4,
          name: '配置成功',
          value: 6
        },{
          id: 5,
          name: '配置失败',
          value: 7
        },*/{
      id: 3,
      name: '激活过期',
      value:3
    }
  ]
}

const mutations = {
  SET_NEWSERVICE_DATA (state, val) {
    state.newServiceData = Object.assign(_.cloneDeep(initData), val)
  },
  SET_ServiceTem (state, value) {
    state.serviceTem = value
  },
  SET_ServieceRightDrawerEvent (state, val) {
    state.rightDrawerEvent = val
  }
}

const getters = {
  chooseYesNoTypeObj: state => () => {
    const obj = {}
    state.chooseYesNoType.forEach(item => {
      obj[item.id] = i18n.t(item.name)
    })
    return obj
  },
  saleStateTypeObj: state => () => {
    const obj = {}
    state.saleStateType.forEach(item => {
      obj[item.id] = i18n.t(item.name)
    })
    return obj
  },
  serviceTemObj: state => () => {
    const obj = {}
    state.serviceTem.forEach(item => {
      obj[item.serviceCode] = item.serviceName
    })
    return obj
  }
}

const actions = {
  getServiceTemData ({ commit }) {
    commit('SET_ServiceTem', [])
    return new Promise((resolve, reject) => {
      serviceConfig.serviceConfigSelect().then(res => {
        commit('SET_ServiceTem', res.data || [])
        resolve(true)
      }).catch(err => {
        reject(err)
      })
    })
  }

}
export default {
  state,
  mutations,
  actions,
  getters
}
